import Taro from "@tarojs/taro";
let Authorization = ''
export default (options = { method: "GET", data: {} }) => {

  if(!Authorization){
    Taro.getStorage({
      key: 'Authorization',
      success (res) {
        Authorization = res.data
      }
    })
  }
  let URL = '?'
  if (options.method.toUpperCase() == 'GET') {
    Object.keys(options.data).map(v => {
      URL += `${v}=${options.data[v]}&`
    })
    URL = URL.split('')
    URL.pop()
    URL = URL.join('')
  }
  return Taro.request({
    url: "https://www.shengyigang.com/taro" + (options.method.toUpperCase() == 'GET' ? options.url + URL : options.url),
    data:
      JSON.stringify(options.data) == "{}" ? "" : JSON.stringify(options.data),
    header: {
      "content-type": "application/json",
      os: 3,
      identification: "sell",
      Authorization: Authorization
    },
    method: options.method.toUpperCase()
  }).then(res => {
    const { statusCode, data } = res;
    if (statusCode >= 200 && statusCode < 300) {
      if (data.code !== 1000) {
        Taro.showToast({
          title: res.data.msg || res.data.code,
          icon: "none",
          mask: true
        });
      }
      return data.data;
    } else {
      throw new Error(`网络请求错误，状态码${statusCode}`);
    }
  });
};
